package com.service;

import com.dao.TableMapper;
import com.pojo.Table;
import com.pojo.TableExample;
import com.pojo.TableStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TableService {
    @Autowired
    TableMapper tableMapper;
    @Autowired
    TableExample example;

    public List<Table> getAll () {
        return tableMapper.selectByExample(null);
    }

    public List<Table> getBySize (Integer size) {
        TableExample.Criteria criteria = example.createCriteria();
        criteria.andTSizeEqualTo(size);
        if (tableMapper.selectByExample(example) == null)
            return null;
        else
            return tableMapper.selectByExample(example);
    }

    public List<Table> getByStatus (String status) {
        TableExample.Criteria criteria = example.createCriteria();
        criteria.andTStatusEqualTo(status);
        if (tableMapper.selectByExample(example) == null)
            return null;
        else
            return tableMapper.selectByExample(example);
    }

    public void addTable (Table table) {
        tableMapper.insertSelective(table);
    }

    public void deleteTable (Integer id) {
        tableMapper.deleteByPrimaryKey(id);
    }

    //这行新增的 ---模糊查询,两个条件 self
    public List<Table> getTableByTwoCondition(String tStatus, Integer tSize) {
        //可能需要new才不会出错
        TableExample tableExample = new TableExample();
        TableExample.Criteria criteria1 = tableExample.createCriteria();
        //进行模糊匹配的 or 操作

        criteria1.andTStatusLike("%" + tStatus + "%");
        //        criteria1.andTStatusLike("%" + tStatus + "%").andTSizeEqualTo(tSize);

        //        TableExample.Criteria criteria2 = tableExample.createCriteria();
        //        criteria2.andTSizeEqualTo(tSize);



        //        tableExample.or(criteria2);
        tableExample.or().andTSizeEqualTo(tSize);

        List<Table> tableList = tableMapper.selectByExample(tableExample);

        if(tableList == null){
            return null;
        }else{
            return tableList;
        }
    }

    // 查看餐桌名是否重复
    public boolean checkTableByTName(String tName){
        TableExample tableExample = new TableExample();
        TableExample.Criteria criteria = tableExample.createCriteria();
        //查看是否相等
        criteria.andTNameEqualTo(tName);
        long count = tableMapper.countByExample(tableExample);
        return count == 0;

    }

    //更新餐桌信息
    public void updateTable(Table tablevo){
        tableMapper.updateByPrimaryKeySelective(tablevo);
    }


    //根据餐桌信息查询餐桌
    public List<Table> selectTableWithStatus(){
        return  tableMapper.selectTableWithStatus(TableStatus.EMPTY.getStatus());
    }


}
